//
//  GROrderCommentViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/24.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GROrderCommentViewController.h"
#import "GROderGoodCommentCell.h"
#import "RequestManager+GROrderManager.h"
#import "GROrderManagerAction.h"
#import "GRStoreAction.h"
@interface GROrderCommentViewController ()<UITableViewDelegate,UITableViewDataSource,GRStarsViewDelegate,GROderGoodCommentCellDelegate>
{
    NSInteger orderStarGrade;
}
@property(nonatomic,strong)NSMutableArray * commentArr;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet GRStarsView *starView;
@property (weak, nonatomic) IBOutlet UIButton *submitBt;

@property (weak, nonatomic) IBOutlet UILabel *startDesLabel;

@end

@implementation GROrderCommentViewController
-(NSMutableArray *)commentArr {
    if (!_commentArr) {
        _commentArr = [NSMutableArray array];
    }
    return _commentArr;
}

-(BOOL)navigationShouldPopOnBackButton{
    [[LFAlertBox defaultAlert]showAlertContent:@"是否放弃评价？" cancle:@"否" sure:@"是" completion:^(NSInteger index) {
        if (index) {
            if (_isRiceOrder) {
                if (_isStoreComment) {
                    [self.billModel.goodsEvaluateImg  removeAllObjects];
                    self.billModel.goodsEvaluate = @"";
                    self.billModel.goodsStarGrade = 0;
                }else{
                [self.riceModel.goodsEvaluateImg  removeAllObjects];
                self.riceModel.goodsEvaluate = @"";
                self.riceModel.goodsStarGrade = 0;
                }
            }else{
            for (GROrderListGoodModel * listModel in self.orderListModel.goods) {
                [listModel.goodsEvaluateImg  removeAllObjects];
                listModel.goodsEvaluate = @"";
                listModel.goodsStarGrade = 0;
            }
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];

    return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"评价";
    self.commentArr = [NSMutableArray arrayWithArray:self.orderListModel.goods];
    self.starView.isComment = YES;
    self.starView.delegate = self;
    if (!_isStoreComment) {
        self.comentList.tableHeaderView = self.headerView;
    }
    [self.comentList registerNib:[UINib nibWithNibName:@"GROderGoodCommentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"commentCell"];
    self.comentList.tableFooterView = [[UIView alloc]init];

}
-(void)selectedStarScore:(CGFloat)start{
    orderStarGrade = start;
    self.submitBt.backgroundColor = HEXCOLOR(0xFBD62D);
    self.submitBt.userInteractionEnabled = YES;
    [self.submitBt setTitleColor:HEXCOLOR(0x222222) forState:UIControlStateNormal];
    self.startDesLabel.text = [GROrderManagerAction getGradeDes:start];
    if (start <= 2) {
        self.startDesLabel.textColor = HEXCOLOR(0x666666);
    }else{
        self.startDesLabel.textColor = HEXCOLOR(0xFB842D);
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GROderGoodCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"commentCell" forIndexPath:indexPath];
    if (_isRiceOrder) {
        commentCell.isRiceOrder = YES;
        if (_isStoreComment) {
            commentCell.isStoreComment = YES;
            commentCell.delegate = self;
            commentCell.billModel = self.billModel;
        }else{
        commentCell.riceModel = self.riceModel;
            commentCell.delegate = self;
        }
    }else{
       commentCell.goodModel = self.commentArr[indexPath.row];
        commentCell.delegate = self;
    }
    commentCell.curetntVc = self;
    return commentCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat count = 1.0;
    if (_isRiceOrder) {
        if (_isStoreComment) {
            count = (self.billModel.goodsEvaluateImg.count+1)/3.0;
            
        }else{
            count = (self.riceModel.goodsEvaluateImg.count+1)/3.0;
            
        }
        return  count >1? 285+(KscreenWidth-32)/3*2:285+(KscreenWidth-32)/3;
    }else{
        GROrderListGoodModel * goodModel = self.commentArr[indexPath.row];
        count = (goodModel.goodsEvaluateImg.count+1)/3;
          return  count >1? 285+(KscreenWidth-32)/3*2:255+(KscreenWidth-32)/3;
    }
}

-(void)changeHeigt:(GROderGoodCommentCell *)cell{
    NSIndexPath  * index = [self.comentList indexPathForCell:cell];
    [self.comentList reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isRiceOrder) {
        return 1;
    }else{
    return self.commentArr.count;
    }
}

-(void)submitRiceOrder{
    NSDictionary * dic ;
    if (_isStoreComment) {
        if (self.billModel.goodsStarGrade <=2 &&self.billModel.goodsEvaluate.length == 0) {
            [GRHUD showMessage:@"请完善商品的评价"];
            return;
        }
        dic = @{
                @"content":self.billModel.goodsEvaluate?self.billModel.goodsEvaluate:@"",
                @"orderId":@(self.billModel.id),
                @"photosArray":self.billModel.goodsEvaluateImg,
                @"rank":@(self.billModel.goodsStarGrade),
                @"type":@"store"
                };
    }else{
        dic = @{
                @"content":self.riceModel.goodsEvaluate,
                @"orderId":@(self.riceModel.id),
                @"photosArray":self.riceModel.goodsEvaluateImg,
                @"rank":@(orderStarGrade),
                @"type":@"discount",
                @"dicountRank":@(self.riceModel.goodsStarGrade),
                @"discountId":@(self.riceModel.discountId)
                };
    }
    [GRStoreAction riceOrderComment:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            [self.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuucess" object:nil];
            [GRHUD showMessage:@"评价成功"];
            if (self.successBlock != nil) {
                self.successBlock();
            }
        }
    }];
}

- (IBAction)submitAction:(id)sender {
    if (_isRiceOrder) {

        [self submitRiceOrder];
    }else{
    NSMutableArray * goodArr = [NSMutableArray array];
    for (GROrderListGoodModel * goodModel in self.commentArr) {
        if (goodModel.goodsStarGrade <=2) {
            if ([goodModel.goodsEvaluate isEqualToString:@""]||goodModel.goodsStarGrade == 0||!goodModel.goodsEvaluate) {
                [GRHUD showMessage:@"请添加文字评价"];
                return;
            }
        }
        NSDictionary * dic = @{
                               @"goodsEvaluate": goodModel.goodsEvaluate?goodModel.goodsEvaluate:@"",
                               @"goodsEvaluateImg":goodModel.goodsEvaluateImg?goodModel.goodsEvaluateImg:@"" ,
                               @"goodsId":@(goodModel.goodsId),
                               @"goodsStarGrade":@(goodModel.goodsStarGrade),
                               @"orderInfoId":@(goodModel.orderInfoId)
                               };
    
        [goodArr addObject:dic];
    }
    NSDictionary * evalutateDic = @{
                                    @"goodsEvaluate":goodArr,
                                    @"orderId":@(self.orderListModel.orderId),
                                    @"orderStarGrade":@(orderStarGrade)
                                    };
    [RequestManager EvaluteOrder:evalutateDic CompleteHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            NSLog(@"评论成功");
            [self.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuucess" object:nil];
           
        }else{
            [GRHUD showProgress:error.errorMsg];
        }
    }];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
